package com.feicheng.iou.controller.student.open;

import com.alibaba.fastjson.JSONObject;
import com.feicheng.iou.commom.ReturnCode;
import com.feicheng.iou.constant.SessionConstant;
import com.feicheng.iou.entity.Student;
import com.feicheng.iou.entity.User;
import com.feicheng.iou.exception.SystemException;
import com.feicheng.iou.pojo.login.LoginParamPOJO;
import com.feicheng.iou.result.ResultInfoV110;
import com.feicheng.iou.service.UserService;
import com.feicheng.iou.service.student.open.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @ClassName StudentOpenController
 * @Description TODO
 * @Author luoxin
 * @Date 2021/8/31 11:38 上午
 */
@RestController
@RequestMapping("/iou/system/student")
@Slf4j
public class StudentOpenController {

    @Autowired
    UserService userService;
    @Autowired
    StudentOpenService studentOpenService;

    /**
     * @param loginParamPOJO
     * @return
     * @Author luoxin
     * @Description 学生端登陆
     * @Date 11:49 上午 2021/8/31
     */
    @PostMapping("/login")
    public ResultInfoV110 queryUserAndPermisson(LoginParamPOJO loginParamPOJO, HttpServletRequest request) {

        log.info("LoginController的queryUserAndPermisson方法进入,参数为" + loginParamPOJO);
        //校验用户名和密码
        if (loginParamPOJO.getAccount() == null || loginParamPOJO.getPassword() == null) {
            throw new SystemException(ReturnCode.ACCOUNT_PASSWORD_NOT_NULL);
        }

        return studentOpenService.selectUserInfoOfLogin(loginParamPOJO, request);
    }

    /**
     * @param
     * @return
     * @Author luoxin
     * @Description 学生端登出
     * @Date 11:49 上午 2021/8/31
     */
    @DeleteMapping("/loginout")
    public ResultInfoV110 logout(HttpServletRequest request) {
        HttpSession session = request.getSession();
        session.removeAttribute(SessionConstant.STUDENT_CURRENT);
        return ResultInfoV110.success("您已成功登出");
    }

    /**
     * @param password,userId
     * @return
     * @Author luoxin
     * @Description TODO
     * @Date 11:55 上午 2021/8/31
     */
    @PutMapping("/password")
    public ResultInfoV110 updatePassword(String password, Integer userId,String openId) {
        log.info("StudentOpenController的updatePassword方法进入,参数密码：" + password);
        return studentOpenService.updatePassword(password, userId,openId);
    }

    @GetMapping("/info/{userId}")
    public ResultInfoV110 selectinfo(@PathVariable("userId") Integer userId) {
        log.info("StudentOpenController的selectinfo方法进入");
        return studentOpenService.selectInfo(userId);
    }

    @PutMapping("/info/update")
    public ResultInfoV110 updateInfo(String studentStr, String userStr) {
        log.info("StudentOpenController的selectinfo方法进入参数为学生信息：" + studentStr + "用户信息：" + userStr);
        //字符串转换Json对象
        JSONObject studentJsonObject = JSONObject.parseObject(studentStr);
        //json对象转换baomingpojo对象
        Student student = JSONObject.toJavaObject(studentJsonObject, Student.class);

        //字符串转换Json对象
        JSONObject userJsonObject = JSONObject.parseObject(userStr);
        //json对象转换baomingpojo对象
        User user = JSONObject.toJavaObject(userJsonObject, User.class);

        return studentOpenService.update(student, user);
    }

    @GetMapping("/payment/{userId}")
    public ResultInfoV110 selectPayment(@PathVariable("userId") Integer userId) {
        log.info("StudentOpenController的selectPayment方法进入");

        return studentOpenService.selectPayment(userId);
    }

}
